﻿// ReSharper disable CheckNamespace
namespace System.Diagnostics // Must be in the namespace where it exists for the full framework.
// ReSharper restore CheckNamespace
{
   // Stopwatch was dropped as of Silverlight 4
   // This doesn't have the high resolution like the one in the full .NET Framework but it's better than nothing.
   public class Stopwatch
   {
      long start;
      long previousElapsed;

      public void Start()
      {
         start = DateTime.Now.Ticks;
         IsRunning = true;
      }

      public void Stop()
      {
         previousElapsed += DateTime.Now.Ticks - start;
         IsRunning = false;
      }

      long CurrentElapsed
      {
         get { return (IsRunning ? DateTime.Now.Ticks - start : 0); }
      }

      public long ElapsedTicks
      {
         get { return previousElapsed + CurrentElapsed; }
      }

      public TimeSpan Elapsed
      {
         get { return new TimeSpan(ElapsedTicks); }
      }

      public double ElapsedMilliseconds
      {
         get { return Elapsed.TotalMilliseconds; }
      }

      public void Reset()
      {
         Stop();
         previousElapsed = 0;
         start = 0;
      }

      public void Restart()
      {
         Reset();
         Start();
      }

      public bool IsRunning { get; private set; }

      public bool IsHighResolution
      {
         get { return false; }
      }

      public static Stopwatch StartNew()
      {
         var stopwatch = new Stopwatch();
         stopwatch.Start();
         return stopwatch;
      }
   }
}
